@extends('backstage.layout.master')
@section('title', '素材列表')
@section('content')
    <div class="breadcrumbs" id="breadcrumbs">
        <script type="text/javascript">
            try {
                ace.settings.check('breadcrumbs', 'fixed')
            } catch (e) {
            }
        </script>

        <ul class="breadcrumb">
            <li>
                <i class="icon-home home-icon"></i>
                <a target="_blank" href="backstage/home">主页</a>
            </li>

            <li class="active">
                <a target="_blank" href="/backstage/source-pic/all">素材列表</a>
            </li>
            <li>
                素材列表
            </li>
        </ul><!-- .breadcrumb -->

        <div class="nav-search" id="nav-search">
        </div><!-- #nav-search -->
    </div>

    <div class="page-content">
        <div class="page-header">
            <h1>
                素材
                <small>
                    <i class="icon-double-angle-right"></i>
                    素材列表
                </small>
            </h1>
        </div>
        <div class="row">
            <div class="col-sm-12">
            <div class="well">
            <form action="javascript:void(0)" name="searchForm">
            <img src="/images/icon_search.gif" width="26" height="22" border="0" alt="SEARCH">
            <!-- 品牌 -->
            审核状态:<select name="audit_state" id="audit_state">
             <option @if($audit_state===-1) selected @endif value=-1>全部</option>
             <option @if($audit_state===0) selected @endif value=0>未提交</option>
             <option @if($audit_state===1) selected @endif value=1>审核中</option>
             <option @if($audit_state===2) selected @endif value=2>已通过</option>
             <option @if($audit_state==3) selected @endif value=3>未通过</option>
            </select>
            <!-- 上架 -->
            上架状态:<select name="is_shelves" id="is_shelves">
             <option @if($is_shelves===-1) selected @endif value=-1>全部</option>
             <option @if($is_shelves===0) selected @endif value=0>下架</option>
             <option @if($is_shelves===1) selected @endif value=1>上架</option>
            </select>
             上传平台:<select name="platform" id="platform">
               <option @if($platform===-1) selected @endif value=-1>全部</option>
               <option @if($platform===1) selected @endif value=1>系统后台</option>
               <option @if($platform===2) selected @endif value=2>设计师或品牌商后台</option>
               <option @if($platform===3) selected @endif value=3>APP</option>
            </select>
            <!-- 关键字 -->
            <input type="button" id="filter_btn" value=" 搜索 " class="button">
            </form>
            </div>
            </div>
            <div class="col-sm-12">
                <h3 class="header smaller lighter red">说明：</h3>
                <div class="well">
                    <h4 class="green smaller lighter">注意点：</h4>
                    1、价格修改需要凌晨才能生效</br>
                </div>
            </div>
            <div class="col-xs-12">
                <!-- PAGE CONTENT BEGINS -->
                <div class="table-responsive">
                    <table id="sample-table-1" class="table table-striped table-bordered table-hover">
                        <thead>
                        <tr>
                            <th class="center" width="40px">
                                <label>
                                    <input type="checkbox" class="ace">
                                    <span class="lbl"></span>
                                </label>
                            </th>
                            <th width="50px">素材编号</th>
                            <th class="hidden-480" width="120px">缩略图</th>
                            <th width="50px">审核状态</th>
                            <th width="50px">上架状态</th>
                            <th width="50px">显示(仅创作列表)</th>
                            <th>修改后台上传素材价格</th>
                            <th width="60px">修改未生效定价</th>
                            <th>素材名称</th>
                            <th width="50px">用户名</th>
                            <th class="hidden-480" width="120px">更新时间</th>
                            <th width="60px">操作区</th>
                        </tr>
                        </thead>

                        <tbody>
                        @foreach ($source_pics as $source_pic)
                        <tr class="" data-tr-id={{isset($source_pic->source_pic_id)?$source_pic->source_pic_id:''}}>
                            <td class="center">
                                <label>
                                    <input type="checkbox" name="tag_id" class="ace" value={{isset($source_pic->id)?$source_pic->id:0}} >
                                    <span class="lbl"></span>
                                </label>
                            </td>
                            <td>
                                {{isset($source_pic->source_pic_id)?$source_pic->source_pic_id:''}}
                            </td>
                            <td class="hidden-480" align="center"><img src="{{isset($source_pic->small_image)?$source_pic->small_image:''}}" width="100" ></td>
                            <td>
                                @if($source_pic->audit_state==0)
                                    还未提交审核
                                @else
                                <select id="aud_{{$source_pic->source_pic_id}}" onchange="changeAuditState('{{$source_pic->source_pic_id}}')">
                                    <option @if($source_pic->audit_state==1) selected @endif value =1 >审核中</option>
                                    <option @if($source_pic->audit_state==2) selected @endif value =2 >已通过</option>
                                    <option @if($source_pic->audit_state==3) selected @endif value =3 >未通过</option>
                                </select>
                                @endif
                            </td>
                            <td align="center" style="background-color: rgb(255, 255, 255);">
                                @if(isset($source_pic->is_shelves)&&($source_pic->is_shelves==1))
                                    <img src="/backstage/images/yes.gif">
                                @else
                                    <img src="/backstage/images/no.gif">
                                @endif
                            </td>
                            <td>
                                <select id="is_show_{{$source_pic->source_pic_id}}" onchange="changeShowState('{{$source_pic->source_pic_id}}')">
                                    <option @if($source_pic->is_show==0) selected @endif value =0 >不显示</option>
                                    <option @if($source_pic->is_show==1) selected @endif value =1 >显示</option>
                                </select>
                            </td>
                            @if(isset($source_pic->is_official)&&($source_pic->is_official))
                            <td style="width:100px;">
                                <input type="number" min=0 step=0.01 name="price" value="{{$source_pic->price}}" class="price" style="width:100px;">
                                <input type="hidden" value="{{$source_pic->price}}">
                                <input type="hidden" value="{{$source_pic->source_pic_id}}">
                            </td>
                            @else
                               <td>不支持修改</td>
                            @endif
                            <td >
                                @if(isset($source_pic->has_modify_price)&&($source_pic->has_modify_price==true))
                                    {{isset($source_pic->modify_price)?$source_pic->modify_price:''}}
                                @else
                                    暂无
                                @endif
                            </td>
                            @if(isset($source_pic->is_official)&&($source_pic->is_official))
                                <td style="width:100px;">
                                    <input type="text" name="source_pic_name" value="{{isset($source_pic->name)?$source_pic->name:''}}" class="source_pic_name" style="width:100px;">
                                    <input type="hidden" value="{{$source_pic->name}}">
                                    <input type="hidden" value="{{$source_pic->source_pic_id}}">
                                </td>
                            @else
                                <td>不支持修改</td>
                            @endif
                            <td>{{isset($source_pic->user_name)?$source_pic->user_name:''}}</td>
                            <td class="hidden-480">
                                {{isset($source_pic->updated_at)?date('Y-m-d H:i:s',$source_pic->updated_at):''}}
                            </td>
                            <td>
                                @if(isset($source_pic->is_official)&&($source_pic->is_official))
                                <div class="visible-md visible-lg hidden-sm hidden-xs btn-group">
                                    <button class="btn btn-xs btn-danger" data-delete="{{isset($source_pic->source_pic_id)?$source_pic->source_pic_id:''}}" title="删除">
                                        <i class="icon-trash bigger-120"></i>
                                    </button>
                                </div>
                                @else
                                    暂不支持修改
                               @endif
                            </td>
                        </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>
                <div class="row">
                    <div class="col-sm-6">
                        <div class="dataTables_info" id="sample-table-2_info"></div>
                    </div>
                    <div class="col-sm-6">
                        <div class="dataTables_paginate paging_bootstrap">
                            <?php echo $source_pics->render(); ?>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-xs-12" style="position: static;height: 50px;">
                        <span class="input-icon">
					   <select id="selActionType"  style="width:209px;">
                            <option value =0>请选择 。。。。</option>
                            <option value =1>归入标签</option>
					   </select>
				     </span>
                        <span class="input-icon" >
                            <select id="selAction"  style="width:209px;">
					        </select>
				        </span>
                        <span class="input-icon">
                        <input  type="button" value="确定" class="btn batch_processing_submit"  style="color:#0a03ff;">
                        </span>
                    </div>
                <!-- PAGE CONTENT ENDS -->
            </div><!-- /.col -->
        </div><!-- /.row -->
    </div><!-- /.page-content -->
@stop
@section('script')
    <script type="text/javascript">
        $(function () {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $('#filter_btn').click(function() {
                var url_para='';
                var audit_state=parseInt($('#audit_state option:selected').val());
                var is_shelves=parseInt($('#is_shelves option:selected').val());
                var platform=parseInt($('#platform option:selected').val());
                if((audit_state==-1)&&(is_shelves==-1)&&(platform==-1)){
                    swal({title: '请先选择', text: "",type: "warning", timer:500 });
                    return;
                }
                window.location.href="/backstage/source-pic/all?audit_state="+audit_state+'&is_shelves='+is_shelves+'&platform='+platform;
            });
            $('[data-rel=tooltip]').tooltip();
            $('[data-delete]').click(function(){
                var  id = $(this).attr("data-delete");
                if(id!==''){
                    swal({
                        title: "确定删除？？",
                        text: "",
                        type: "warning",
                        showCancelButton: true,
                        confirmButtonColor: "#DD6B55",
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        closeOnConfirm: true,
                        closeOnCancel: true
                    },function (){
                        $.ajax({
                            type: "POST",
                            url: "/backstage/source-pic/del-source-pic",
                            data: {'id':id},
                            success: function (msg) {
                                if(msg.state=1){
                                    swal({title: "删除成功！", text: "",type: "success", timer:1000 });
                                    $('[data-tr-id='+id+']').remove();
                                }else {
                                    swal({title: msg.message, text: "",type: "error", timer:1000 });
                                }

                            }, error: function () {
                                swal({title: "系统繁忙！", text: "",type: "error", timer:1000 });
                            }
                        });
                    });
                }
            });
            $('table th input:checkbox').on('click' , function(){
                var that = this;
                $(this).closest('table').find('tr > td:first-child input:checkbox')
                        .each(function(){
                            this.checked = that.checked;
                            $(this).closest('tr').toggleClass('selected');
                        });

            });
            $('#selActionType').change(function() {
                var sel_action_type=parseInt($(this).val());
                switch (sel_action_type)
                {
                    case 1:
                        getResourceTags();
                        break;
                    default:
                        return false;
                        break;
                }
                return true;
            });
            $('.batch_processing_submit').click(function() {
                var data = new Array();
                var i=0;
                var sel_action_type=$('#selActionType option:selected').val();
                var sel_action=$('#selAction option:selected').val();
                sel_action_type=parseInt(sel_action_type);
                $('table th input:checkbox').closest('table').find('tr > td:first-child input:checkbox')
                    .each(function(){
                        var is_checked=$(this).is(":checked");
                        if(is_checked==true&&(tag_id!=0)){
                            var tag_id=$(this).closest('tr').attr("data-tr-id");
                            data.push(tag_id);
                        }
                    });
                if((sel_action_type==1)&&(data.length>0) ){
                    $.ajax({
                        type: "POST",
                        url: "/backstage/source-pic/batch-update-source-pic",
                        data: {'type':sel_action_type,'type_value':sel_action,'data':data},
                        success: function (msg) {
                            if(msg.state=1){
                                swal({title: '修改成功', text: "",type: "success", timer:1000 });
                                location.reload();
                            }else {
                                swal({title: msg.message, text: "",type: "error", timer:1000 });
                            }

                        }, error: function () {
                            swal({title: "系统繁忙！", text: "",type: "error", timer:1000 });
                        }
                    });
                }
            });
            $(".price").blur(function(){
                var price = $(this).val();
                var id = $(this).next().next().val();
                var a = $(this);
                $.ajax({
                    type: "POST",
                    url: "/backstage/source-pic/update-price",
                    data:{"price":price,"id":id},
                    success: function(msg){
                        if(msg.state==1){
                            swal({
                                title: "更新成功！",
                                text: "",
                                type: "success",
                                timer:2000
                            });
                        }else {
                            swal({
                                title: msg.message,
                                text: "",
                                type: "error",
                                timer:2000
                            });
                        }
                    },
                    error:function(){
                        swal({
                            title: "系统繁忙稍后再试",
                            text: "",
                            type: "error",
                            timer:2000
                        });
                    }
                });
            });
            var update_source_pic_state=true;
            $(".source_pic_name").blur(function(){
                var source_pic_name = $(this).val();
                var id = $(this).next().next().val();
                if(!update_source_pic_state){
                    swal('处理中，请稍后','','warning');
                }
                update_source_pic_state=false;
                $.ajax({
                    type: "POST",
                    url: "/backstage/source-pic/update-source-pic-name",
                    data:{"source_pic_name":source_pic_name,"id":id},
                    success: function(msg){
                        if(msg.state==1){
                            swal({
                                title: "更新成功！",
                                text: "",
                                type: "success",
                                timer:2000
                            });
                        }else {
                            swal({
                                title: msg.message,
                                text: "",
                                type: "error",
                                timer:2000
                            });
                        }
                    },
                    error:function(){
                        swal({
                            title: "系统繁忙稍后再试",
                            text: "",
                            type: "error",
                            timer:2000
                        });
                    }
                });
                update_source_pic_state=true;
            });
        });
        function changeAuditState(source_pic_id) {
            var audit_state=$('#aud_'+source_pic_id+' option:selected').val();
            if (audit_state==3){
                window.location.href='/backstage/source-pic/audit-not-pass?source_pic_id='+source_pic_id;
            }else if(audit_state==2||audit_state==1){
                swal({
                    title: "确定修改？？",
                    text: "",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    closeOnConfirm: true,
                    closeOnCancel: true
                },function (){
                    $.ajax({
                        type: "POST",
                        url: "/backstage/source-pic/audit-pass",
                        data: {'source_pic_id':source_pic_id,'audit_state':audit_state},
                        success: function (msg) {
                            if(msg.state==1){
//                                swal({title: "修改成功！", text: "",type: "success", timer:1000 });
                                showMessage('','修改成功');
                                location.reload();
                            }else {
//                                swal({title: msg.message, text: "",type: "error", timer:1000 });
                                showMessage(msg.message,'');
                            }

                        }, error: function () {
                            swal({title: "系统繁忙！", text: "",type: "error", timer:1000 });
                        }
                    });
                });
            }
        }
        var show_state=true;
        function changeShowState(source_pic_id) {
            var is_show=$('#is_show_'+source_pic_id+' option:selected').val();
            if(!show_state){
                swal({title: "请刷新！", text: "",type: "success", timer:500 });
                show_state=true;
                return;
            }
            show_state=false;
            $.ajax({
                type: "POST",
                url: "/backstage/source-pic/change-show",
                data: {'source_pic_id':source_pic_id,'is_show':is_show},
                success: function (msg) {
                    if(msg.state==1){
                        showMessage('','修改成功');
                    }else {
                        showMessage(msg.message,'');
                    }

                }, error: function () {
                    swal({title: "系统繁忙！", text: "",type: "error", timer:1000 });
                }
            });
            show_state=true;
        }
        function getResourceTags() {
            $.ajax({
                type: "POST",
                url: "/backstage/source-pic/source-pic-resource-tags",
                data: {},
                success: function (msg) {
                    console.log(msg.data);
                    var html='';
                    if(msg.state=1){
                        for(var a=0;a<msg.data.length;a++) {
                            html+='<option value="' + msg['data'][a].tag_id + '">'+ msg['data'][a].tag_name +'</option><br />';
                        }
                        $('#selAction').html(html);
                    }else {
                        swal({title: msg.message, text: "",type: "error", timer:1000 });
                    }

                }, error: function () {
                    swal({title: "系统繁忙！", text: "",type: "error", timer:1000 });
                }
            });
        }
    </script>
@stop